//
//  YFNavigationTopView.m
//  QuanJieShopProject
//
//  Created by apple on 2018/4/2.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "YFNavigationTopView.h"
#import "YFSearchBar.h"
#import "UIImageView+WebCache.h"
#import "YFArrowButton.h"

@interface YFNavigationTopView()<UITextFieldDelegate>
@end

@implementation YFNavigationTopView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

//        _isShowLeftBtn = YES;
        [self makeView];
    }
    return self;
}

- (void)updateCircleImage {
    
//    UserInfoModel *model = [QJUserInfo getCurrentUser];
//    if (model) {
//        
//        // 判断后台给的图片有无值
//        if ([[NSString stringWithoutNilFormart:model.avatar] isEqualToString:@""]) {
//            [self.rightBtn setImage:[UIImage imageNamed:@"user-3"] forState:UIControlStateNormal];
//        }else {
//            // 网络请求图片
//            UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",NET_BASEURL_IMG,model.avatar]]]];
//            UIImage *circleImage = [UIImage circleImage:img withInset:0];
//            [self.rightBtn setImage:circleImage forState:UIControlStateNormal];
//        }
//
//    }else{
//        [self.rightBtn setImage:[UIImage imageNamed:@"user-3"] forState:UIControlStateNormal];
//    }
}

- (void)layoutSubviews {

    [self.rightBtn layoutButtonWithEdgeInsetsStyle:LLButtonStyleTextBottom imageTitleSpace:0];

//    [self.leftBtn setTitle:@"未获取" forState:UIControlStateNormal];
}

- (void)makeView {
    
    UIView *statusBarB= [[UIView alloc] initWithFrame:(CGRect){0,0,UISCREEN_WIDTH,Height_StatusBar}];
    statusBarB.backgroundColor = YFColor_Red;
    [self addSubview:statusBarB];
    
    UIView *viewBack = [[UIView alloc] initWithFrame:(CGRect){0,Height_StatusBar,UISCREEN_WIDTH,44}];
    viewBack.backgroundColor = statusBarB.backgroundColor;
    [self addSubview:viewBack];
    
    //        CAGradientLayer *gradientLayer = [[CAGradientLayer alloc] init];
    //        gradientLayer.locations = @[@(0.0),@(1.0)];
    //        gradientLayer.colors = @[(__bridge id)[UIColor clearColor].CGColor,(__bridge id)[UIColor colorWithWhite:0.290 alpha:1.000].CGColor];
    //        gradientLayer.frame = viewBack.bounds;
    //        gradientLayer.startPoint = CGPointMake(0, 1.0);
    //        gradientLayer.endPoint = CGPointMake(0, 0);
    //        [viewBack.layer addSublayer:gradientLayer];
    
    
    // 位置 YFArrowButton
    YFArrowButton *leftBtn = [[YFArrowButton alloc] init];
    [leftBtn addTarget:self action:@selector(locationClick) forControlEvents:UIControlEventTouchUpInside];
//    NSLog(@"localCity = %@", self.localCity);
    [leftBtn setTitle:@"未获取" forState:UIControlStateNormal];
    leftBtn.titleLabel.textColor = [UIColor whiteColor];
    leftBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    leftBtn.selected = NO;
//    leftBtn.backgroundColor = [UIColor lightGrayColor];
    [viewBack addSubview:leftBtn];
    self.leftBtn = leftBtn;
    
    // 搜索框
    YFSearchBar *searchBar = [[YFSearchBar alloc] init];
    self.searchBar = searchBar;
    searchBar.backgroundColor = [UIColor whiteColor];
    searchBar.font = [UIFont systemFontOfSize:14];
    searchBar.textColor = [UIColor colorWithHexString:@"999999"];
    searchBar.text = @"搜你喜欢";
    searchBar.layer.borderWidth = 0.5;// 边框
    searchBar.layer.borderColor = [UIColor colorWithHexString:@"CCCCCC"].CGColor;
    searchBar.layer.cornerRadius = 5;
    [viewBack addSubview:searchBar];
    // 放大镜
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search"]];
    searchBar.leftView = imgView;
    searchBar.delegate = self;
    searchBar.leftViewMode = UITextFieldViewModeAlways;
    
    // 扫一扫
    UIImageView *htimgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"weweima"]];
    searchBar.rightView = htimgView;
    //        htimgView.backgroundColor = [UIColor redColor];
    searchBar.rightViewMode = UITextFieldViewModeAlways;
    htimgView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesturRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(htimgViewClick)];
    [htimgView addGestureRecognizer:tapGesturRecognizer];
    
    // 右边按钮
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //        [loginBtn setBackgroundImage:[UIImage imageNamed:@"user"] forState:UIControlStateNormal];
    [rightBtn setImage:[UIImage imageNamed:@"category"] forState:UIControlStateNormal];
    [rightBtn setTitle:@"分类" forState:UIControlStateNormal];
    rightBtn.backgroundColor = statusBarB.backgroundColor;
    [rightBtn addTarget:self action:@selector(personnalClick) forControlEvents:UIControlEventTouchUpInside];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:10];
    [viewBack addSubview:rightBtn];
//    [rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    rightBtn.backgroundColor = [UIColor whiteColor];
    self.rightBtn = rightBtn;
    
    // 初始化就更新头像
    [self updateCircleImage];
    
    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@80);
        make.height.equalTo(@(viewBack.frame.size.height));
        make.left.equalTo(viewBack.mas_left);
        make.centerY.equalTo(viewBack.mas_centerY);
    }];
    
//    leftBtn.frame = CGRectMake(0, 0, 80, viewBack.frame.size.height);
    [leftBtn layoutIfNeeded];
    
    CGFloat rightBtnHW = viewBack.frame.size.height * 1;
    [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(rightBtnHW));
        make.height.equalTo(@(rightBtnHW));
        make.right.equalTo(viewBack.mas_right);
        make.centerY.equalTo(leftBtn.mas_centerY);
    }];
    
    CGFloat searchBarH = viewBack.frame.size.height * 0.7;
    [searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(rightBtn.mas_left);
        make.left.equalTo(leftBtn.mas_right);
        make.height.equalTo(@(searchBarH));
        make.centerY.equalTo(leftBtn.mas_centerY);
    }];
}

//- (void)setIsShowLeftBtn:(BOOL)isShowLeftBtn {
//    
//    _isShowLeftBtn = isShowLeftBtn;
//    
//    if (!_isShowLeftBtn) {
//        
//        // 重新计算frame
//        [self.searchBar mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(self.leftBtn.mas_left).with.offset(10);
//        }];
//        
//        self.leftBtn.hidden = YES;
//    }
//}

- (void)personnalClick {
    if (self.rightBtnClick) {
        self.rightBtnClick();
    }
}



- (void)locationClick {
    
    self.leftBtn.selected =  self.leftBtn.selected ? NO : YES;
    
    if (self.leftBtnClick) {
        self.leftBtnClick();
    }
}

- (void)htimgViewClick {
    if (self.codeClick) {
        self.codeClick();
    }
}

- (void)setLeftBtn:(UIButton *)leftBtn {
    _leftBtn = leftBtn;
    NSLog(@"hidden = %d", leftBtn.hidden);
    // 重新计算frame
    [self.searchBar mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.leftBtn.mas_left).with.offset(10);
    }];
    if (leftBtn.hidden) {
        

//        self.leftBtn.hidden = YES;
    }else {
        
    }
}

- (void)setHiddenLeftButton:(BOOL)hiddenLeftButton {
    _hiddenLeftButton = hiddenLeftButton;
    
    self.leftBtn.hidden = hiddenLeftButton;
    
    // 重新计算frame
    [self.searchBar mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.leftBtn.mas_left).with.offset(10);
    }];
}

- (void)setHiddenRightButton:(BOOL)hiddenRightButton {
    _hiddenRightButton = hiddenRightButton;
    
    self.rightBtn.hidden = hiddenRightButton;
    // 重新计算frame
    [self.searchBar mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.rightBtn.mas_right).with.offset(-10);
    }];
}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

    //    KPushViewControllerAction(VC);
    if (self.textFieldClick) {
        self.textFieldClick();
    }
    
    return NO;
}

@end
